# Dickstein, Ho, and Mark (2023)
# This script runs the counterftual code that estimates the counterfactuals presented in the paper using the counterfactual algorithm outlined in Appendix F. 

# The files in the folder "library" are the main scripts that conduct the counterfactual. These scripts are called by other scripts in the specs, loop_specs, and bootstrap folders to estimate the counterfactuals. The script beginning in DA03b (which, in turn, calls the script beginning in DA02) collects household characteristics, choice sets, estimated demand parameters, and estimated price setting parameters. The script DA03aRunCounterfactualsFuctions.R defines the functions that runs the counterfactual algorithm outlined in Appendix section F.1.
#To run the main counterfactual results, use the code in the subfolder specs/lowrisk_simplemh_censor0.2. Here, run the script DB01RunFixedMarkupCounterfactuals, which in turn calls the script DA03cRunCounterfactuals1table.R for each of the markups that we report counterfactuals for in the paper. DA03cRunCounterfactuals1table.R is the script where most of the parameters assumed in the counterfactual algorithm are defined. This script calls the files beginning in DA03a and DA03b to get the data and relevant functions ready. Then, it calls the script DA03dCounterfactualCode.R to implement the counterfactual algorithm. Last, it saves the results of the counterfactuals. The last two files in this folder beginning in DA04 and DA05 need to be run sequentially to generate estimates of surplus in the small group market under a variety of assumptions.
#The files in the loop_specs subfolder generate the counterfactual results used to construct figures 5 through 7. Each of these files estimates numerous counterfactuals under different assumptions, calling the files starting in DA03a, DA03b, and DA03d referred to above in a for loop. Similarly, the files in the bootstrap subfolder generate the bootstrap counterfactual runs used to construct the bootstrap estimates of the counterfactual results.

cd specs/lowrisk_simplemh_censor0.2
Rscript DB01RunFixedMarkupCounterfactuals.R
Rscript DA04CreateSGSurplusData.R
mkdir -p ../../../tablesandfigures/release/fee_sg_cs
stata -b DA05EstimateSGSurplus.do

# Estimating the loop results:
cd ../../loop_specs
Rscript EA01RunMultipleCounterfactuals_final.R

# To run the bootstrap estimation procedure, run the following scripts
# cd ../bootstrap
# Rscript EA01RunBootstrapCounterfactuals.R
# Rscript EA02CollectingBootstrapResults.R
